package store.lunangangster.nodeflow.util;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import store.lunangangster.nodeflow.common.BaseConstant;
import store.lunangangster.nodeflow.enums.NodeType;
import store.lunangangster.nodeflow.exception.ExecuteException;

/**
 * Slot中key标识生成器
 *
 * @author cwk
 * @version 1.0
 */
public class KeyGenerator {
  private static final Logger log = LoggerFactory.getLogger(KeyGenerator.class);

  private static final Map<NodeType, String> PREFIX_MAP = new HashMap<NodeType, String>() {
    {
      put(NodeType.NODE_COMMON, BaseConstant.THEN);
      put(NodeType.NODE_IF, BaseConstant.IF);
      put(NodeType.NODE_FOR, BaseConstant.FOR);
      put(NodeType.NODE_WHILE, BaseConstant.WHILE);
      put(NodeType.NODE_EXIT, BaseConstant.EXIT);
      put(NodeType.NODE_SWITCH, BaseConstant.SWITCH);
    }
  };

  public static String generate(NodeType nodeType) {
    String prefix = "_" + PREFIX_MAP.get(nodeType) + "#";
    if (nodeType == null || StrUtil.isBlank(prefix)) {
      log.error("Id generate fail: nodeType is not defined");
      throw new ExecuteException("Id generate fail: nodeType is not defined");
    }
    return prefix + UUID.randomUUID().toString(true);
  }
}
